<?php

require_once APPLICATION_PATH.'\models\Users.php';
require_once APPLICATION_PATH.'\models\PublishSubscribe.php';

/**
 * This controller deals with all AJAX requests of subscription/unsubscription
 * to a topic, and of publishion of a new message.
 * @author Christophe Sourisse
 */
class PublishSubscribeController extends Zend_Controller_Action
{

    /**
     * This controller only accepts AJAX request. All other requests are redirected
     * towards the Search controller (where the user can find topics to subscribe
     * to).
     */
    public function init()
    {
        /* Initialize action controller here */
       // Only deal with AJAX requests, others are redirected towards 'index' action.
       if (!$this->_request->isXmlHttpRequest ()) {
          $this->_redirect('/search/');
       }
       
       // Turn off view rendering & layout
       $this->_helper->viewRenderer->setNoRender(true);
       $this->_helper->layout->disableLayout(true);
    }

    /**
     * Thus, no index action.
     */
    public function indexAction()
    {       
       // and DIE !
       die;
    }

    /**
     * The current user subscribes to a topic.
     */
    public function subscribeAction()
    {
       // Get the topic to subscribe to from the AJAX request
       $avatar = $this->_request->getParam("avatar");
       $topicToSubscribe = Model_Users::getUserBySurname($avatar);
       
       // Get the session
       $session = Zend_Registry::get("my_session_namespace");
       
       // Register the new post into the database & the session
       Model_PublishSubscribe::addIDToTopicsString($session->user["topics"], $topicToSubscribe["id"]);
       $n = Model_PublishSubscribe::updateUserTopics($session->user["id"], $session->user["topics"]);
       
       // Acknowledge the success
       if ($n == 1)
          echo json_encode(array('success' => true, 'tts' => $topicToSubscribe["id"], "sut" => $session->user["topics"]));
       else {
          $session->user["topics"] = Model_PublishSubscribe::retrieveIDFromTopicsString($session->user["topics"],$topicToSubscribe["id"]);
          echo json_encode(array('success' => false, 'n' => $n, 'tts' => $topicToSubscribe["id"], "sut" => $session->user["topics"]));
       }
       
       // and DIE !
       die;
    }

    /**
     * The current user unsubscribes to a topic.
     */
    public function unsubscribeAction()
    {
       // Get the topic to subscribe to from the AJAX request
       $avatar = $this->_request->getParam("avatar");
       $topicToUnsubscribe = Model_Users::getUserBySurname($avatar);
       
       // Get the session
       $session = Zend_Registry::get("my_session_namespace");
       
       // Register the new post into the database & the session
       $session->user["topics"] = Model_PublishSubscribe::retrieveIDFromTopicsString(
               $session->user["topics"],
               $topicToUnsubscribe["id"]
       );
       $n = Model_PublishSubscribe::updateUserTopics($session->user["id"], $session->user["topics"]);
       
       // Acknowledge the success
       if ($n == 1)
          echo json_encode(array('success' => true));
       else {
          $session->user["topics"] = Model_PublishSubscribe::addIDToTopicsString(
                  $session->user["topics"],
                  $topicToSubscribe["id"]
          );
          echo json_encode(array('success' => false));
       }
       
       // and DIE !
       die;
    }
    
    /**
     * The current user publishes a message on his own page, or on the page of
     * another topic.
     * 
     *@author Christophe Sourisse, Dominik Imsirovic
     */
    public function publishAction()
    {
        // Get the message from the AJAX request
        $newpost = $this->_request->getParam("post");
        $avatar = $this->_request->getParam("avatar");
          // Get the session
        $session = Zend_Registry::get("my_session_namespace");
        // Control user input
        if (strlen($newpost) > 0 & strlen($newpost) <= 120) {
           
           // Get the author ID, i.e. the one of the current user
           $author_ID = $session->user["id"];

           // Get the ID on which the current user wrote his post
           if ($avatar == "") {
              $topic_ID = $session->user["id"];
           }
           else {
              $topic = Model_Users::getUserBySurname($avatar);
              $topic_ID = $topic["id"];
           }

           // Register the new post into the database
           Model_PublishSubscribe::registerPost($newpost, $author_ID, $topic_ID);

           // Acknowledge the success
           echo json_encode(array('success' => true));
           
           
         
        }
        else {
           // Acknowledge the failure
           echo json_encode(array('success' => false));
        }
        
        //refresh Page
        // $uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
       
        // ?avatar='.$this->_request->getParam("avatar").'&data='.$this->_request->getParam("data").'&count='.$this->_request->getParam("count"));
        // and DIE !
        die;
        
    }

}


